package com.itheima.interceptor;

import com.itheima.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class PrivilegeInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //判断session 中有没有user对象，来判断用户有没有登录
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if(user == null){//session存在user,放行
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return false;
        }

        return true;

    }
}
